﻿using FreeSql.DataAnnotations;
using GalaSoft.MvvmLight;
using Newtonsoft.Json;
using PropertyChanged;
using sys_Print.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using static FreeSql.Internal.GlobalFilter;

namespace sys_Print.DataAsset
{
    [Serializable]
    [AddINotifyPropertyChangedInterface]
    [JsonObject(MemberSerialization.OptIn)]
    public partial class PrintCanvasModel : ObservableObject, IDataErrorInfo
    {
        [JsonProperty, Column(Name = "model_id", IsPrimary = true)]
        public int ModelId { get; set; }

        [JsonProperty, Column(Name = "project_id", IsPrimary = true, IsIdentity = true)]
        public int ProjectId { get; set; }

        [Required(ErrorMessage = "模块内容是必填字段")]
        [JsonProperty, Column(DbType = "varchar(255)")]
        public string Contents { get; set; } = string.Empty;

        [Required(ErrorMessage = "模块类型是必填字段")]
        [JsonProperty, Column(Name = "controls", DbType = "varchar(255)")]
        public string ControlType { get; set; } = string.Empty;

        [Required(ErrorMessage = "模块X坐标是必填字段")]
        [JsonProperty, Column(Name = "coordinate_x")]
        public double CoordinateX { get; set; }

        [Required(ErrorMessage = "模块Y坐标是必填字段")]
        [JsonProperty, Column(Name = "coordinate_y")]
        public double CoordinateY { get; set; }

        [JsonProperty, Column(Name = "font_size")]
        public double FontSize { get; set; }

        [JsonProperty, Column(Name = "height")]
        public double? Height { get; set; }

        [JsonProperty, Column(Name = "isdisplay")]
        public bool IsdisplayKeyword { get; set; }

        [JsonProperty, Column(Name = "is_reverse")]
        public bool IsReverse { get; set; }

        [Required(ErrorMessage = "项目名称是必填字段")]
        [JsonProperty, Column(Name = "project_name", DbType = "varchar(255)")]
        public string ProjectName { get; set; } = string.Empty;

        [Required(ErrorMessage = "模块宽度是必填字段")]
        [Range(0, double.MaxValue, ErrorMessage = "宽度必须大于或等于0")]
        [JsonProperty, Column(Name = "width")]
        public double Width { get; set; }

        [JsonProperty, Column(Name = "task_change")]
        public bool TaskChange { get; set; }

        [Required(ErrorMessage = "加载顺序是必填字段")]
        [Range(1, int.MaxValue, ErrorMessage = "加载顺序必须大于或等于1")]
        [JsonProperty, Column(Name = "order_no")]
        public int OrderNo { get; set; }

        [JsonProperty, Column(Name = "meter_change")]
        public bool MeterChange { get; set; }

        [JsonProperty, Column(IsIgnore = true)]
        public ObservableCollection<ContentEntries> DataGridLists { get; set; }

        [JsonProperty, Column(IsIgnore = true)]
        public ContentType ContentType { get; set; }

        [JsonProperty, Column(IsIgnore = true)]
        public double RowHeight { get; set; }

        [JsonProperty, Column(IsIgnore = true)]
        public bool IsChanged { get; set; }

        public string Error => null;

        public string this[string columnName]
        {
            get
            {
                var context = new ValidationContext(this, null, null) { MemberName = columnName };
                var results = new System.Collections.Generic.List<ValidationResult>();
                if (!Validator.TryValidateProperty(this.GetType().GetProperty(columnName).GetValue(this, null), context, results))
                {
                    return results[0].ErrorMessage;
                }
                return null;
            }
        }

        public void AcceptChanges()
        {
            IsChanged = false;
        }

        public ObservableCollection<ContentEntries> GetDataGridLists(Meter meter, TaskInfo taskInfo,bool firstIteration )
        {
            ObservableCollection<ContentEntries> lists = new ObservableCollection<ContentEntries>();

            foreach (ContentEntries item in this.DataGridLists)
            {
                item.AttriName = item.AttriName;

                if (item.ContentChange || firstIteration)
                {
                    //任务信息
                    object propertyValue = typeof(TaskInfo).GetProperty(item.AttributeName)?.GetValue(taskInfo, null);

                    if (propertyValue == null)
                    {
                        //标定参数
                        propertyValue = typeof(Meter).GetProperty(item.AttributeName)?.GetValue(meter, null);
                    }

                    item.Contents = propertyValue is null ? item.Contents : propertyValue.ToString();

                    //仪表系数单位路径获取不同
                    if (item.AttributeName == "MeterK")
                    {
                        //string meterKUnit = taskInfo.MeterKUnit.Replace("3", "³");
                        //meterKUnit = meterKUnit.Replace("-1", "⁻¹");

                        item.AttriName = $"{item.Prefix}{item.Contents}{item.Suffix}{taskInfo.MeterKUnit}";
                    }
                    else
                    {
                        // 将单位中的 "3" 替换为 "³"
                        //string unit = item.Unit?.Replace("3", "³");
                        item.AttriName = $"{item.Prefix}{item.Contents}{item.Suffix}{item.Unit}";
                    }

                }

                if (item.IsEmptyHide && item.Contents == null)
                {
                    lists.Add(item);
                }
            }

            foreach(ContentEntries item in lists)
            {
                this.DataGridLists.Remove(item);
            }

            return this.DataGridLists;
        }

        public double GetRowHeight()
        {
            double totalHeight = (double)Height * 3.78; // 你需要在 ViewModel 中提供控件的高度
            int rowCount = 1;
            if (DataGridLists != null)
            {
                rowCount = DataGridLists.Count;
            }

            double rowheight = totalHeight / rowCount;

            return (double)(rowheight > 60 ? 60 : rowheight);
        }
    }

}
